package com.yuan.model_convenient.service.impl;

import com.yuan.model_convenient.domain.ConvenientRepair;
import com.yuan.model_convenient.repository.ConvenientRepairDao;
import com.yuan.model_convenient.service.ConvenientRepairService;
import com.yuan.model_convenient.service.dto.ConvenientRepairDto;
import com.yuan.model_convenient.service.dto.ConvenientRepairQueryCriteria;
import com.yuan.model_convenient.service.mapstruct.ConvenientRepairMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class ConvenientRepairServiceImpl implements ConvenientRepairService {

    private final ConvenientRepairDao convenientRepairDao;

    private final ConvenientRepairMapper convenientRepairMapper;

    @Override
    public void download(List<ConvenientRepairDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConvenientRepairDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<ConvenientRepairDto> queryAll(Pageable pageable) {
        Page<ConvenientRepair> page = convenientRepairDao.findAll(pageable);
        for (ConvenientRepair convenientRepair : page) {
            convenientRepair.setLiverName(convenientRepairDao.findUserNameById(convenientRepair.getLiverId()));
            convenientRepair.setLiverPhone(convenientRepairDao.findUserPhoneById(convenientRepair.getLiverId()));
        }
        return PageUtil.toPage(page.map(convenientRepairMapper::toDto));
    }

    @Override
    public PageResult<ConvenientRepairDto> queryAllByType(ConvenientRepairQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientRepair> all = convenientRepairDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(all.map(convenientRepairMapper::toDto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConvenientRepair resources) {
        convenientRepairDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConvenientRepair resources) throws Exception {
        Optional<ConvenientRepair> byId = convenientRepairDao.findById(resources.getRepairId());
        if (byId.isPresent()) {
            ConvenientRepair convenientRepair = byId.get();
            BeanUtils.copyProperties(resources, convenientRepair);
            convenientRepairDao.save(convenientRepair);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        convenientRepairDao.deleteById(ids);
    }
}
